//
//  LZReceiveRestoreViewController.m
//  laziz_Consumer
//
//  Created by xbnzd on 17/3/22.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZReceiveRestoreViewController.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "LAReviewTableViewCell.h"
#import "LAReviewModel.h"
#import "LZMsgCenterPresenter.h"
#import "extobjc.h"

@interface LZReceiveRestoreViewController ()<UITableViewDelegate,UITableViewDataSource,LAReviewCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) LZMsgCenterPresenter *msgPresenter;
@property (nonatomic, strong) UIButton *nav_left_back;

@end

@implementation LZReceiveRestoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureUI];
    
    if (self.reviewType == ReviewType_Video) {
        [self getVidoReplyDataAction];
    }else{
        [self getRestaurantReplyDataAction];
    }
}

#pragma mark - UI
- (void)configureUI
{
    self.umLogPageViewName = @"评论商家收到回复";
    self.title = LZLocalizedString(@"mssage_reply_detail", nil);
    
    _dataArr = @[].mutableCopy;
    _msgPresenter = [[LZMsgCenterPresenter alloc] init];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    self.tableView.estimatedRowHeight = 44.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LAReviewTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

#pragma mark - data

/**
 请求餐厅回复
 */
- (void)getRestaurantReplyDataAction
{
    @weakify(self);
    [self startLoadingViewForView:self.view loadingViewUserInteractionEnabled:YES];
    [self.msgPresenter getRestaurantReplyWithReplyID:self.replyId Success:^(LAReviewModel *model) {
        
        @strongify(self);
        [self stopLoadingViewForView:self.view];
        [self.dataArr addObject:model];
        [self.tableView reloadData];
        
    } Failure:^(NSString *fail) {
        @strongify(self);
        [self stopLoadingViewForView:self.view];
    }];
}


/**
 请求视频回复
 */
- (void)getVidoReplyDataAction
{
    @weakify(self);
    [self startLoadingViewForView:self.view loadingViewUserInteractionEnabled:YES];
    [self.msgPresenter getVideoReplyWithReplyID:self.replyId Success:^(LAReviewModel *model) {
        
        @strongify(self);
        [self stopLoadingViewForView:self.view];
        [self.dataArr addObject:model];
        [self.tableView reloadData];

    } Failure:^(NSString *fail) {
        @strongify(self);
        [self stopLoadingViewForView:self.view];
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LAReviewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [self configureCell:cell atIndexPath:indexPath];
    
    return cell;
}

- (void)configureCell:(LAReviewTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.fd_enforceFrameLayout = NO; // Enable to use "-sizeThatFits:"
    
    cell.indexPath = indexPath;
    cell.delegate = self;
    cell.reviewType = self.reviewType;
    cell.headImageType = HeadImageType_User;
    cell.isMessageCenter = YES;
    cell.entity = self.dataArr[indexPath.section];
}

#pragma mark - UITableViewDelegate

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    float height = [tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(LAReviewTableViewCell *cell) {
//        
//        [cell getReviewLabel].numberOfLines = 0;
//        
//        [self configureCell:cell atIndexPath:indexPath];
//    }];
//    
//    return height;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)refreshCellHeight:(NSIndexPath *)indexPath {
    
    [self.tableView reloadData];
}


@end
